package me.jiatao.mvpdagger2;

import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.List;

import javax.inject.Inject;

import me.jiatao.mvpdagger2.entity.UserInfo;
import me.jiatao.mvpdagger2.ioc.DaggerUserInfoComponent;
import me.jiatao.mvpdagger2.ioc.NewsModule;
import me.jiatao.mvpdagger2.ioc.UserInfoModules;
import me.jiatao.mvpdagger2.mvc.base.BaseMVPActivity;
import me.jiatao.mvpdagger2.mvc.news.INewsContract;
import me.jiatao.mvpdagger2.mvc.news.NewsPresenter;
import me.jiatao.mvpdagger2.mvc.userinfo.IUserInfoContract;
import me.jiatao.mvpdagger2.mvc.userinfo.UserInfoPresenter;

public class MainActivity extends BaseMVPActivity<UserInfoPresenter>
        implements IUserInfoContract.IUserInfoView, INewsContract.INewsView {

    @Inject
    NewsPresenter newsPresenter;

    private ProgressDialog progressDialog;

    public void getUserInfo(View view) {
        getPresenter().getUsers();
        newsPresenter.getNewsList();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_main);

        progressDialog = new ProgressDialog(MainActivity.this);//1.创建一个ProgressDialog的实例
        progressDialog.setMessage("正在加载中，请稍等......");//3.设置显示内容
        progressDialog.setCancelable(false);//4.设置可否用back键关闭对
    }

    @Override
    protected void injectPresenter() {
        DaggerUserInfoComponent.builder()
                .userInfoModules(new UserInfoModules(this))
                .newsModule(new NewsModule(this))
                .build()
                .inject(this);


    }


    @Override
    public void onLoading() {
        progressDialog.show();
    }

    @Override
    public void onError() {
        progressDialog.hide();
        Toast.makeText(this, "获取用户信息错误", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onSucceed(UserInfo userInfo) {
        progressDialog.hide();
        Toast.makeText(this, userInfo.toString(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNewsSucceed(List<String> newsList) {
        Log.i("***onNewsSucceed***", newsList.toString());
    }

    public void gotoActivity2(View view) {
        Intent intent = new Intent(this, Main2Activity.class);
        startActivity(intent);
    }
}
